package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.mapper.CustomerReservationMapper;
import com.zzyl.service.CustomerReservation;

import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import com.zzyl.vo.VisitVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CustomerReservationImpl implements CustomerReservation {

    private final CustomerReservationMapper customerReservationMapper;


    @Override
    public Integer getCancelCount(Long updateBy) {
       Integer cancelCount  = customerReservationMapper.getCancelCount(updateBy);
       return cancelCount;
    }

    @Override
    public void insert(ReservationDto reservationDto) {
        Reservation reservation = new Reservation();
        BeanUtil.copyProperties(reservationDto,reservation);
        reservation.setStatus(0);
        customerReservationMapper.insert(reservation);
    }

    @Override
    public PageResponse<ReservationVo> pageList(Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        List<ReservationVo> list = customerReservationMapper.pageList(status);
        Page<ReservationVo> page = (Page<ReservationVo>) list;
        return PageResponse.of(page, ReservationVo.class);
    }

    @Override
    public void cancel(Long id) {
        customerReservationMapper.cancel(id);
    }

    @Override
    public List<TimeCountVo> countByTime(Long time) {
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedTime = sdf.format(date);
        List<TimeCountVo> list = customerReservationMapper.countByTime(formattedTime);
        for (TimeCountVo timeCountVo : list) {
            Integer count = timeCountVo.getCount();
            Integer remainCount = 6 - count;
            timeCountVo.setCount(remainCount);
        }
        return list;
    }
}
